/**
 * Created by Administrator on 2018/5/3.
 */
var studel=function(sid){
    $.ajax({
        url:"users/del",
        data:"sid="+sid,
        type:"POST",
        success:function(data){
            alert(data);
            $('#table').bootstrapTable('refresh', {url: 'users/findAll'});
        }
    });
}

var edit=function(sid){
    $.ajax({
        url:"users/findById",
        data:"sid="+sid,
        type:"POST",
        success:function(data){
            $("#student_update").find("input[name='id']").val(data.id);
            $("#student_update").find("input[name='name']").val(data.name);
            $("#student_update").find("input[name='age']").val(data.age);
            $("#student_update").find("input[name='birthday']").val(data.birthday);
            $("#student_update").find("input[name='phone']").val(data.phone);
            $("#student_update").find("select[name='gradeId']").val(data.gradeId);
            /*弹出窗体*/
            $("#myModalLabel2").text("编辑");
            $('#myModal2').modal();
        }
    });
}


var resetform=function(fobj){
    fobj.find("input[name='name']").val("");
    fobj.find("input[name='age']").val("");
    fobj.find("input[name='birthday']").val("");
    fobj.find("input[name='phone']").val("");
}
var tableObject=new Object();
tableObject.init=function(){


    var queryParam=function(params){
        var temp = {   //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
            limit: params.limit,   //页面大小
            offset: params.offset,  //页码
            name: $("#txt_name").val(),
            startbirthday: $("#txt_start_birthday").val(),
            endbirthday:$("#txt_end_birthday").val()
        };
        return temp;
    }

    var $table = $('#table');
    $table.bootstrapTable({
        url: "users/findAll",
        dataType: "json",
        pagination: true, //分页
        cache: false,
        striped: true,
        strictSearch: true,
        locale:"zh-CN",
        queryParams:queryParam,
        singleSelect: false,
        pageSize:5,
        search: false, //显示搜索框
        sidePagination: "server", //服务端处理分页
        columns: [
            {
                title: '用户ID',
                field: 'userId',
                align: 'center',
                valign: 'middle'
            },
            {
                title: '权限',
                field: 'role',
                align: 'center',
                valign: 'middle',
            },
            {
                title: '真实姓名',
                field: 'name',
                align: 'center'
            },
            {
                title: '手机号',
                field: 'phone',
                align: 'center'
            },
            {
                title: '注册时间',
                field: 'gradeId',
                align: 'center',
            },
            {
                title: '操作',
                field: 'id',
                align: 'center',
                formatter:function(value,row,index){
                    var e = '<a href="#" mce_href="#" onclick="edit(\''+ row.id + '\')">编辑</a> ';
                    var d = '<a href="#" mce_href="#" onclick="studel(\''+ row.id +'\')">删除</a> ';
                    return e+d;
                }
            }
        ],
        responseHandler:function(res){
            return res;
        }
    });
}

$(function(){
    tableObject.init();
    var t=tableObject;

    $("#btn_query").click(function(){
        $('#table').bootstrapTable('refresh', {url: 'users/findAll'});
    })

    $("#btn_add").click(function () {
             $("#myModalLabel").text("新增");
             $('#myModal').modal();
    });

    $("#btn_add_reset").click(function(){
        resetform($("#student_add"));
    });
    $("#btn_add_submit").click(function(){
        var p=$("#student_add").serialize();
        jQuery.ajax({
            type:"POST",
            url:"users/save",
            data:p.toString(),
            success:function(data){
                alert(data);
                resetform($("#student_add"));
                $('#table').bootstrapTable('refresh', {url: 'users/findAll'});
            }
        });
    })


    $("#btn_update_submit").click(function(){
        var p=$("#student_update").serialize();
        jQuery.ajax({
            type:"POST",
            url:"users/update",
            data:p.toString(),
            success:function(data){
                alert(data);
                resetform($("#student_update"));
                $('#table').bootstrapTable('refresh', {url: 'users/findAll'});
            }
        });
    })
});